##
# \file bjmath.py
#

# --  Python imports  --
from math import floor, sqrt
# -- End Python imports --

# --  Pybrain imports  --
# -- End Pybrain imports --

# --  Project imports  --
from namedcards import Cards
from hand import Hand
# -- End Project imports --

##
# \class BJMath
# \details This class consists of the common computations involved in the game of blackjack.
class BJMath:

    ##
    # \details Returns the value of the supplied hand
    # \pre none
    # \post none
    # \param hand The hand of the player from which to calculate its value.
    # \return The value of the given hand.
    @staticmethod
    def calcHandValue(hand):
        num_aces = hand.count(Cards.ACE)
        value    = sum(hand)

        while ( (value > Hand.BLACKJACK) and (num_aces > 0) ):
            value    -= 10
            num_aces -= 1

        return value

    ##
    # \details Returns the type of the supplied hand
    # \pre none
    # \post none
    # \param hand The hand of the player from which to calculate its type.
    # \return The type of the given hand.
    @staticmethod
    def calcHandType(hand):
        num_aces = hand.count(Cards.ACE)
        value    = sum(hand)

        while ( (value > Hand.BLACKJACK) and (num_aces > 0) ):
            value    -= 10
            num_aces -= 1

        if num_aces > 0:
            # Soft
            return Hand.SOFT
        elif num_aces == 0:
            # Hard
            if len(hand) == 2:
                if hand[0] == hand[1]:
                    # Pair
                    return Hand.PAIR
                else:
                    return Hand.HARD
            else:
                return Hand.HARD
        else:
            assert False, "Hand has negative number of Aces!"
